package com.print.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.print.domain.Item;
import com.print.domain.User;
import com.print.domain.UserItem;

public class ItemDAOImpl implements ItemDAO{

	private HibernateTemplate hibernateTemplate;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	@Override
	public void saveItem(Item item) {
		hibernateTemplate.saveOrUpdate(item);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Item> listItems() {
		return hibernateTemplate.find("from Item");
	}
	
	public List<Item> getItemOrdinati(String username) {
		List<Item> tmp = (List<Item>)hibernateTemplate.find("select item from Item item, UserItem useritem, User user where item = useritem.item and useritem.user = user and user.username = ? and useritem.quantita > 0", username);
		for (Item item : tmp) {
			for (UserItem userItem : item.getUserItems()) {
				System.out.println(userItem.getIdUserItem());
			}
		}
		return tmp;
	}
	
	public List<Item> getItemStampati(String username) {
		return (List<Item>)hibernateTemplate.find("select item from Item item, UserItem useritem, User user where item = useritem.item and useritem.user = user and user.username = ? and useritem.quantita = 0", username);
	}
	
	@Override
	public Item getItemByImage(String image) {
		List<Item> list = (List<Item>) this.hibernateTemplate.find("from Item i where i.image = ?", image);
		if(list != null)
			return list.get(0);
		return null;
	}
	
	@Override
	public void updateItem(Item item) {
		this.hibernateTemplate.update(item);
	}
	
	@Override
	@SuppressWarnings("unchecked")
	public Item getItem(Long id) {
		List<Item> items = hibernateTemplate.find(
				"from Item item where item.idItem = ?", id);
		if (!items.isEmpty())
			return items.get(0);
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean deleteItem(Long id) {
		try {
			Item item = getItem(id);

			List<UserItem> userItems = hibernateTemplate
					.find("from UserItem useritem" + " where useritem.item = ?",
							item);
			for (UserItem userItem : userItems) {
				hibernateTemplate.delete(userItem);
			}

			hibernateTemplate.delete(item);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String itemStampati() {
		String string = "";
		List<User> users = (List<User>) hibernateTemplate
				.find("select user " + " from User user, UserItem useritem "
						+ " where user = useritem.user and "
						+ " useritem.quantita = 0");
		users = deleteDuplicateUser(users);
		for (User user : users) {
			System.out.println("utenti = " + user.getUsername());
			List<Item> items = (List<Item>) hibernateTemplate.find(
					"select item" + " from Item item, UserItem useritem "
							+ " where item = useritem.item and "
							+ " useritem.user.idUser = ? and "
							+ " useritem.quantita = 0", user.getIdUser());
			items = deleteDuplicateItem(items);
			for (Item item : items) {
				List<UserItem> useritems = (List<UserItem>) hibernateTemplate
						.find("select useritem " + " from UserItem useritem "
								+ " where useritem.item = ? and "
								+ " useritem.user = ?", item, user);
				for (UserItem userItem : useritems) {
					string += user.getUsername() + "*" + item.getName() + "*"
							+ item.getPrezzoOrdine() + "*" + userItem.getDate()
							+ "#";
				}
			}
		}
		return string;
	}
    
	@SuppressWarnings("unchecked")
	@Override
	public String itemOrdinati() {
		List<UserItem> userItems = (List<UserItem>) hibernateTemplate.find("from UserItem useritem where useritem.quantita > 0");
		String string = "";
		for (UserItem userItem : userItems) {
			string += userItem.getUser().getUsername() + "*" + userItem.getItem().getName() + "*"
					+ userItem.getItem().getPrezzoOrdine() * userItem.getQuantita()
					+ "*" + userItem.getDate() + "*"
					+ userItem.getQuantita() + "*" + userItem.getItem().getImage() + "#";
		}
		
//		String string = "";
//		List<User> users = (List<User>) hibernateTemplate
//				.find("select user " + " from User user, UserItem useritem "
//						+ " where user = useritem.user and "
//						+ " useritem.quantita > 0");
//		users = deleteDuplicateUser(users);
//		for (User user : users) {
//			System.out.println("utenti = " + user.getUsername());
//			List<Item> items = (List<Item>) hibernateTemplate.find(
//					"select item" + " from Item item, UserItem useritem "
//							+ " where item = useritem.item and "
//							+ " useritem.user.idUser = ? and "
//							+ " useritem.quantita > 0", user.getIdUser());
//			items = deleteDuplicateItem(items);
//			for (Item item : items) {
//				List<UserItem> useritems = (List<UserItem>) hibernateTemplate
//						.find("select useritem" + " from UserItem useritem "
//								+ " where useritem.item = ? and "
//								+ " useritem.user = ?", item, user);
//				for (UserItem userItem : useritems) {
//					string += user.getUsername() + "*" + item.getName() + "*"
//							+ item.getPrezzoOrdine() * userItem.getQuantita()
//							+ "*" + userItem.getDate() + "*"
//							+ userItem.getQuantita() + "*" + item.getImage() + "#";
//				}
//			}
//		}
		return string;
	}
    
	@SuppressWarnings("unchecked")
	@Override
	public String itemOrdinatiStampati() {
		String string = "";
		List<User> users = (List<User>) hibernateTemplate.find("select user "
				+ " from User user, UserItem useritem "
				+ " where user = useritem.user");
		users = deleteDuplicateUser(users);
		for (User user : users) {
			System.out.println("utenti = " + user.getUsername());
			List<Item> items = (List<Item>) hibernateTemplate.find(
					"select item" + " from Item item, UserItem useritem "
							+ " where item = useritem.item and "
							+ " useritem.user.idUser = ? ", user.getIdUser());
			items = deleteDuplicateItem(items);
			for (Item item : items) {
				List<UserItem> useritems = (List<UserItem>) hibernateTemplate
						.find("select useritem " + " from UserItem useritem "
								+ " where useritem.item = ? and "
								+ " useritem.user = ?", item, user);
				for (UserItem userItem : useritems) {
					if (userItem.getQuantita() == 0) {
						string += user.getUsername() + "*" + item.getName()
								+ "*" + item.getPrezzoOrdine() + "*"
								+ item.getPrezzoStampa() + "*"
								+ userItem.getDate() + "*"
								+ userItem.getQuantita() + "#";
					} else {
						string += user.getUsername() + "*" + item.getName()
								+ "*" + item.getPrezzoOrdine()
								* userItem.getQuantita() + "*"
								+ item.getPrezzoStampa() + "*"
								+ userItem.getDate() + "*"
								+ userItem.getQuantita() + "#";
					}
				}
			}
		}
		return string;
	}

	private List<User> deleteDuplicateUser(List<User> users) {
		List<User> newList = new ArrayList<User>();
		for (User user : users) {
			if (!newList.contains(user))
				newList.add(user);
		}
		return newList;
	}

	private List<Item> deleteDuplicateItem(List<Item> items) {
		List<Item> newList = new ArrayList<Item>();
		for (Item item : items) {
			if (!newList.contains(item))
				newList.add(item);
		}
		return newList;
	}
}